package com.cheng.controller;


import com.cheng.dao.UavMapper;
import com.cheng.dao.UserMapper;
import com.cheng.pojo.Location;
import com.cheng.pojo.Road;
import com.cheng.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/uav")
@Controller
public class UserController {
    @Autowired
    UserMapper userMapper;
    @RequestMapping("/noauth")
    public String noauth(){
        return "error/noauth";
    }

    @ResponseBody
    @RequestMapping("/hello")
    public String Hello(){
        return "hello";
    }
    @RequestMapping({"/","/toLogin"})
    public String toLogin(){
        return "login";
    }
    //处理表单的登录请求
    @RequestMapping("/login")
    public String login(String username, String password, Model model){
        //使用shiro，编写认证操作
        // 1. 获取Subject
        Subject subject = SecurityUtils.getSubject();
        //2. 封装用户的数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //3. 执行登录的方法，只要没有异常就代表登录成功！
        try {
            subject.login(token); //登录成功！返回首页
            return "redirect:/uav/user/index?msg=yes";
        } catch (UnknownAccountException e) { //用户名不存在
            model.addAttribute("msg","用户名不存在");
            return "login";
        } catch (IncorrectCredentialsException e) { //密码错误
            model.addAttribute("msg","密码错误");
            return "login";
        }
    }

    @RequestMapping("/logout")
    public String logout(){
        // 1. 获取Subject
        Subject subject = SecurityUtils.getSubject();
        // 2. 注销即可!
        subject.logout();
        return "redirect:/uav/";
    }
    @RequestMapping("/register")
    public String register(Model model,String username,String password,String Cpassword){
        if(!password.equals(Cpassword)){
            model.addAttribute("msg","注册失败");
            return "login";
        }else if (password.equals(Cpassword)){
            int user = userMapper.InsertUser(new User(1, username, password, "user"));
            if(user>0) {
                model.addAttribute("msg", "注册成功");
                return "login";
            }
        }
        model.addAttribute("msg","注册失败");
        return "login";
    }

    @RequestMapping("/user/manger")
    public String user(Model model,String username,String nowPage,String limitPage,String id,String msg){
        if(msg!=null&&msg.equals("yes")) model.addAttribute("msg", "修改成功!");
        else if(msg!=null&&msg.equals("no"))model.addAttribute("msg", "修改失败!");
        else if(msg!=null&&msg.equals("Dyes"))model.addAttribute("msg", "删除成功!");
        else if(msg!=null&&msg.equals("Dno"))model.addAttribute("msg", "删除失败!");
        else if(msg!=null&&msg.equals("Ayes"))model.addAttribute("msg", "添加成功!");
        else if(msg!=null&&msg.equals("Ano"))model.addAttribute("msg", "添加失败!");
        if(username=="")username=null;
//        System.out.println(nowPage+":nowPage ");
        int limitPageInt=9;
        if(limitPage!=null)limitPageInt=Integer.valueOf(limitPage);
        if(nowPage==null)nowPage="0";
        Map m=new HashMap();
//        System.out.println(roadCrossing);
        m.put("username",username);
        int count=userMapper.getUser(m).size();
//        System.out.println("count: "+count);
        int pageSize=count/limitPageInt+1;
        int now=Integer.parseInt(nowPage)<0?0:Integer.parseInt(nowPage);
        now=now>pageSize?pageSize:now;
        m.put("nowPage",now*limitPageInt);
        m.put("limitPageInt",limitPageInt);
        List<User> list=userMapper.getUser(m);
        model.addAttribute("limitPage",limitPageInt);
        model.addAttribute("users",list);
        model.addAttribute("nowPage",now);
        model.addAttribute("username",username);
        if(id==null)id="0";
        if(id=="")id="0";
        model.addAttribute("id",id);
//        System.out.println(roadCrossing);
        return "user";
    }

    @RequestMapping("/user/manger/update")
    public String updateUser(User user,Model model){
        int i = userMapper.updateUser(user);
        if(i>0)
        return "redirect:/uav/user/manger?id=1&msg=yes";
        return "redirect:/uav/user/manger?id=1&msg=no";
    }

    @RequestMapping("/user/manger/delete")
    public String deleteUser(User user,Model model){
        int i = userMapper.deleteUser(user.getId());
        if(i>0)
            return "redirect:/uav/user/manger?id=1&msg=Dyes";
        return "redirect:/uav/user/manger?id=1&msg=Dno";
    }
    @RequestMapping("/user/manger/add")
    public String deleteAdd(Model model,String username,String password,String Cpassword){
        if(!password.equals(Cpassword))return "redirect:/uav/user/manger?id=1&msg=Ano";
        int i = userMapper.InsertUser(new User(1,username,password,"user"));
        if(i>0)
            return "redirect:/uav/user/manger?id=1&msg=Ayes";
        return "redirect:/uav/user/manger?id=1&msg=Ano";
    }


}

